$(document).ready(function () {
		loadtab();
	})
	function  loadtab()
	{
	var columns=[[
	              {field:'check',checkbox:true},
	              {field:'proids',title:'产品ids',hidden:true},
	              {field:'proname',title:'产品名称',width:100,align:'center'},
	              {field:'typeids',title:'类别ids',hidden:true},
	              {field:'typename',title:'类别名称',width:100,align:'center'},
	              {field:'number',title:'退货数量',width:100,align:'center',
	            	  editor:{type:'numberspinner',
							options:{
								onSpinUp:function()
								{	
									var n=$(this).numberspinner('getValue');
									sum(null,n);
								},
	              				onSpinDown:function()
	              				{
	              					var n=$(this).numberspinner('getValue');
									sum(null,n);
	              				}
							}
	            	  }
	              },
	              {field:'price',title:'单价（元）',width:100,align:'center'},
	              {field:'total',title:'应退金额（元）',width:100,align:'center',
	            	  formatter: function(value,row,index){
					  		return  row.total=value;
		            }
	              },
	              {field:'refund',title:'实退金额（元）',width:100,align:'center',
	            	  editor:{type:'numberspinner',
						options:{
								onSpinUp:function()
								{	
									var n=$(this).numberspinner('getValue');
									diff(null,n);
								},
				              onSpinDown:function()
			    				{
			    					var n=$(this).numberspinner('getValue');
			    					diff(null,n);
			    				}
							}
	            	  },
	            	  formatter: function(value,row,index){
					  		return  row.differenceresion=value;
		            }
	              },
	              {field:'difference',title:'差异',width:100,align:'center'},
	              {field:'differenceresion',title:'差异原因',width:100,align:'center',editor:'textbox',
	            	  formatter: function(value,row,index){
				  		return  row.differenceresion="";
	            	  }
	              }
	              ]];
	
	$('#refund_datagrid').datagrid({
		singleSelect: true,
		fitColumns:true,
		rownumbers: true,
		method: 'post',
		 columns:columns,
		onClickRow:onClickRow,
		selectOnCheck:$(this).is(':checked'),
		checkOnSelect:$(this).is(':checked'),
		onAfterEdit:function(index,filed,value)
		{	
			var row=$("#refund_datagrid").datagrid("getRows")[index];
			sum(index,row.number);
			diff(index,row.refund);
		}
	});
	
	}
	
	function  sum(index,n)
	{	
		var  row;
		if(index==null)
		{
			var rows = $("#refund_datagrid").datagrid("getRows")
			row = $('#refund_datagrid').datagrid('getSelected');
			index=$('#refund_datagrid').datagrid('getRowIndex', row);
			var sum=parseFloat(row.price)*n;
			var dif=parseFloat(row.refund)-sum;
			$('#refund_datagrid').datagrid('updateRow', {
	            index: index,
	            row: {
	            	number:n,
	            	total:sum,
	            	difference:dif
	            }
	        });
			$('#refund_datagrid').datagrid('beginEdit', index);
		}else
		{
			row=$("#refund_datagrid").datagrid("getRows")[index];
			var sum=parseFloat(row.price)*n;
			var dif=parseFloat(row.refund)-sum;
			$('#refund_datagrid').datagrid('updateRow', {
	            index: index,
	            row: {
	            	number:n,
	            	total:sum,
	            	difference:dif
	            }
	        });
		}
		
	}
	function  diff(index,n)
	{	
		var  row;
		if(index==null)
		{
			var rows = $("#refund_datagrid").datagrid("getRows")
			row = $('#refund_datagrid').datagrid('getSelected');
			index=$('#refund_datagrid').datagrid('getRowIndex', row);
			var dif=n-parseFloat(row.total);
			$('#refund_datagrid').datagrid('updateRow', {
	            index: index,
	            row: {
	            	refund:n,
	            	difference:dif
	            }
	        });
			$('#refund_datagrid').datagrid('beginEdit', index);
		}else
		{
			row=$("#refund_datagrid").datagrid("getRows")[index];
			var dif=n-parseFloat(row.total);
			$('#refund_datagrid').datagrid('updateRow', {
	            index: index,
	            row: {
	            	refund:n,
	            	difference:dif
	            }
	        });
		}
		
		
	}
	
//获取顾客信息
function searchCus() {
	var reData = getCusInfo();
	if (reData.msg == 200) {
		var customerIds = reData.customer.ids;
		//清空表格
		var rows = $('#refund_datagrid').datagrid('getRows');
		for (var i = 0; i < rows.length; i++) {
			var index = $('#refund_datagrid').datagrid('getRowIndex', rows[i]);
			$('#refund_datagrid').datagrid('deleteRow', index);
		}
		$('#refund_datagrid').datagrid('loadData', {total : 0,rows : []});
		
		var url = '/brefund/getReturnProduct';
		var data = {"customerIds" : reData.customer.ids};
		var result = common_ajax.ajaxFunc(url, data, "json");
		if (result != null && result.msg == 200) {
			$('#refund_datagrid').datagrid({
				data:result.cpList
			});
		}
	}
}


//确认退款
function confirm(){
	$("#allData").html("");
	var checkRows=$('#refund_datagrid').datagrid('getChecked');
	for(var i=0;i<checkRows.length;i++)
	{	
		 var rowIndex = $('#refund_datagrid').datagrid('getRowIndex', checkRows[i]);
		 $('#refund_datagrid').datagrid('endEdit', rowIndex);
		var proids = checkRows[i].proids;
		var typeids = checkRows[i].typeids;
		var number = checkRows[i].number;
		var price = checkRows[i].price;
		var total = checkRows[i].total;
		var refund = checkRows[i].refund;
		var difference = checkRows[i].difference;
		var differenceresion = checkRows[i].differenceresion;
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].productIds' value='"+proids+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].proType' value='"+typeids+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].num' value='"+number+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].price' value='"+price+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].amountDue' value='"+total+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].actualAmount' value='"+refund+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].difference' value='"+difference+"'/>");
		$("#allData").append("<input type='hidden' name='productReturnList["+i+"].differenceResion' value='"+differenceresion+"'/>");
	}	
	if(checkRows.length>0)
		{
			var url='/brefund/saveReturn';
			var data=$('#allData_form').serialize();
			var reData = common_ajax.ajaxFunc(url,data);
			if(reData == "200")
			{
				 $.slideMsg("保存成功", parent.$);
				 parent.refresh();
			}else
			{
				$.alert(parent.$, reData);
			}
		 
		}else
		{
			$.alert(parent.$, "未选择退货产品，请先添加");
		}
  
}
/*数据表格行编辑*/
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#refund_datagrid').datagrid('validateRow', editIndex)){
		$('#refund_datagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
	} else {
		return false;
	}
}
function onClickRow(index){
	if (editIndex != index){
		if (endEditing()){
			$('#refund_datagrid').datagrid('selectRow', index)
				.datagrid('beginEdit', index);
				editIndex = index;
		} else {
			$('#refund_datagrid').datagrid('selectRow', editIndex);
		}
	}
}
$(document).keydown(function(event){ 
	if(event.keyCode == 13){
		$('#refund_datagrid').datagrid("endEdit", editIndex);
	} 
});
